﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Things.Web.SaleSet
{
    public partial class SetParameter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _ctlHideGoodsId.Value = Request.QueryString["GoodsId"];
                _ctlHideReferrer.Value = Request.UrlReferrer.ToString();

                Guid goodsId = new Guid(_ctlHideGoodsId.Value);

                Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
                if (parameter != null)
                {
                    this._ctlEditP1.Text = parameter.P1.ToString();
                    this._ctlEditn.Text = parameter.N.ToString();
                    this._ctlEditPn.Text = parameter.PN.ToString();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Guid goodsId = new Guid(_ctlHideGoodsId.Value);
            Things.OrderPlan.Models.SetParameter parameter = new Things.OrderPlan.SetParameterRepository().Find(goodsId);
            if (parameter == null)
            {
                parameter = new Things.OrderPlan.Models.SetParameter();
                parameter.Id = Guid.NewGuid();
                parameter.GoodsId = new Guid(_ctlHideGoodsId.Value);
            }

            parameter.P1 = Convert.ToDecimal(this._ctlEditP1.Text);
            parameter.N = Convert.ToInt32(this._ctlEditn.Text);
            parameter.PN = Convert.ToDecimal(this._ctlEditPn.Text);

            Things.OrderPlan.SetParameterRepository repository = new OrderPlan.SetParameterRepository();
            if (repository.Store(parameter))
            {
                Response.Write("<script>alert('添加成功!');window.location.href='" + _ctlHideReferrer.Value + "';</script>");
            }
            else
            {
                Response.Write("<script>alert('添加失败!');</script>");
            }
        }

        protected void _ctlBtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(_ctlHideReferrer.Value);
        }
    }
}